package com.ss.service.support;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ss.core.exceptions.SummerExecuteException;
import com.ss.entity.SummerFlow;
import com.ss.entity.SummerInstance;
import com.ss.entity.SummerNode;
import com.ss.mapper.SummerFlowMapper;
import com.ss.mapper.SummerNodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 审核操作节点相关方法
 * @Author: 夏十七
 * @Date: 2019/7/28 17:38
 * @Version 1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ExecuteNode {

    @Autowired
    @SuppressWarnings("all")
    private SummerNodeMapper summerNodeMapper;

    /**
     * 获取第一个节点
     * @param flowId 流id
     * @return
     */
    public SummerNode getFirstNode(Integer flowId){
        //查询条件
        QueryWrapper<SummerNode> query = Wrappers.query();
        query.eq("flow_id",flowId);
        query.isNull("pre_node");
        SummerNode node = summerNodeMapper.selectOne(query);
        if (node == null){
            throw new SummerExecuteException("该事件流未找到第一个节点");
        }
        return node;
    }

    /**
     * 获取下一个节点
     * @param nowNode 当前节点
     * @return
     */
    public SummerNode getNextNode(SummerNode nowNode){
        //查询条件
        QueryWrapper<SummerNode> query = Wrappers.query();
        query.eq("id",nowNode.getNextNode());
        SummerNode node = summerNodeMapper.selectOne(query);
        return node;
    }

    /**
     * 获取上一个节点
     * @param nowNode 当前节点
     * @return
     */
    public SummerNode getLastNode(SummerNode nowNode){
        //查询条件
        QueryWrapper<SummerNode> query = Wrappers.query();
        query.eq("id",nowNode.getPreNode());
        SummerNode node = summerNodeMapper.selectOne(query);
        return node;
    }




    /**
     * 获取指定节点
     * @param nodeId 节点id
     * @return
     */
    public SummerNode getAppointNode(Integer nodeId){
        //查询条件
        QueryWrapper<SummerNode> query = Wrappers.query();
        query.eq("id",nodeId);
        SummerNode node = summerNodeMapper.selectOne(query);
        if (node == null){
            throw new SummerExecuteException("该事件流未找到指定节点");
        }
        return node;
    }






}
